package com.ljh.dao;

import com.ljh.pojo.UserBook;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

public class UserBookMapperImpl extends HibernateDaoSupport implements UserBookMapper {
    @Autowired
    private SessionFactory sessionFactory;
    private Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }
    public int addUserBook(UserBook userBook) {
        System.out.println(userBook);
        getSession().save(userBook);
        return 0;
    }

    public int deleteUserBook(UserBook userBook) {
        System.out.println(userBook);
        getSession().delete(userBook);
        return 0;
    }

    public int updateUserBook(UserBook userBook) {
        getSession().createQuery("update UserBook set bookcount=bookcount+1 where id=? and bookid=?")
                .setParameter(0,userBook.getId())
                .setParameter(1,userBook.getBookid())
                .executeUpdate();
        return 0;
    }

    public List<UserBook> queryAllUserBook() {
        return (List<UserBook>)getSession().createQuery("from UserBook").list();
    }

    public List<UserBook> queryAllUserBookById(String id) {
        return (List<UserBook>)getSession().createQuery("from UserBook where id=?").setParameter(0,id).list();
    }

    public List<UserBook> queryAllUserBookByIdByName(UserBook userBook) {
        return (List<UserBook>)getSession().createQuery("from UserBook where id=? and bookid=?").setParameter(0,userBook.getId()).setParameter(1,userBook.getBookid()).list();
    }
}
